#include"../mqserver/mq_exchanger.hpp"
#include<gtest/gtest.h>
mq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment{
    public:
        virtual void SetUp() override{
            emp = std::make_shared<mq::ExchangeManager>("./a/db_file");
            // std::cout<<"create file success"<<std::endl;
        }
        virtual void TearDown() override{
            // std::cout<<"clear..."<<std::endl;
            emp->clear();
        }
};

// TEST(exchange_test,insert_test){
//     std::unordered_map<std::string,std::string> tmpmap = {{"k1","v1"},{"k2","v2"}};
//     emp->declare("exchange1",mq::ExchangeType::DIRECT,true,false,std::unordered_map<std::string,std::string>());
//     emp->declare("exchange2",mq::ExchangeType::DIRECT,true,false,tmpmap);
//     emp->declare("exchange3",mq::ExchangeType::DIRECT,true,false,tmpmap);
//     emp->declare("exchange4",mq::ExchangeType::DIRECT,true,false,std::unordered_map<std::string,std::string>());
// }

TEST(exchange_test,select_test){
    ASSERT_EQ(emp->exists("exchange1"),true);
    ASSERT_EQ(emp->exists("exchange2"),false);
    ASSERT_EQ(emp->exists("exchange3"),true);
    ASSERT_EQ(emp->exists("exchange4"),false);

    ASSERT_EQ(emp->size(),2);
    mq::Exchange::ptr it = emp->select("exchange1");
    ASSERT_EQ(it->name,"exchange1");
    ASSERT_EQ(it->type,mq::ExchangeType::DIRECT);
    ASSERT_EQ(it->durable,true);
    ASSERT_EQ(it->auto_delete,false);
    ASSERT_EQ(it->getargsstr(),std::string(""));

    mq::Exchange::ptr it3 = emp->select("exchange3");
    ASSERT_EQ(it3->name,"exchange3");
    ASSERT_EQ(it3->type,mq::ExchangeType::DIRECT);
    ASSERT_EQ(it3->durable,true);
    ASSERT_EQ(it3->auto_delete,false);
    ASSERT_EQ(it3->getargsstr(),std::string("k1=v1&k2=v2&"));
}

// TEST(exchange_test,remove_test)
// {
//     emp->deleteExchange("exchange2");
//     mq::Exchange::ptr exctmp = emp->select("exchange2");
//     ASSERT_EQ(exctmp.get(),nullptr);
//     ASSERT_EQ(emp->exists("exchange2"),false);

//     emp->deleteExchange("exchange4");
//     mq::Exchange::ptr exctmp4 = emp->select("exchange4");
//     ASSERT_EQ(exctmp.get(),nullptr);
//     ASSERT_EQ(emp->exists("exchange4"),false);
//     ASSERT_EQ(emp->size(),2);
// }   

int main(int argc,char* argv[]){
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    return RUN_ALL_TESTS();
}
